package cn.bob.pet.service.impl;

import cn.bob.basic.query.BaseQuery;
import cn.bob.basic.utils.PageList;
import cn.bob.org.domain.Department;
import cn.bob.pet.domain.PetDetail;
import cn.bob.pet.domain.PetType;
import cn.bob.pet.mapper.PetDetailMapper;
import cn.bob.pet.mapper.PetTypeMapper;
import cn.bob.pet.service.IPetTypeService;
import cn.bob.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author bob
 * @since 2022-10-28
 */
@Service
public class PetTypeServiceImpl extends BaseServiceImpl<PetType> implements IPetTypeService {
    @Autowired
    private PetTypeMapper petTypeMapper;

    @Override
    public List<PetType> petTypetree() {
        List<PetType> all = petTypeMapper.findAll();
        List<PetType> petTypes =new ArrayList<>();
        //将id与对象作为kv存储起来
        Map<Long, PetType> fater = all.stream().collect(Collectors.toMap(PetType::getId, e -> e));
        //循环所有
        for (PetType petType : all) {
            if (null == petType.getPid()){
                //如果父级为空就直接添加
                petTypes.add(petType);
            }else {
                //有父级在map中查询到父级
                PetType peta = fater.get(petType.getPid());
                if (peta!=null){
                    //如果查询到有父类就添加到父类的集合中
                    peta.getChildren().add(petType);
                }
            }
        }
        return petTypes;
    }

}
